package com.hspedu.newData.dmsxl.数组.模拟;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-28 
 * @Version: 1.0
 */

// todo
public class LC54螺旋矩阵 {

    public List<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> list = new ArrayList<>();
        int left = 0;
        int top = 0;
        int right = matrix[0].length - 1;
        int bottom = matrix.length - 1;
        while(left <= right || top <= bottom){
            if (right == left && top == bottom){
                list.add(matrix[right][top]);
            }
            for (int i = left; i < right; i++) {
                list.add(matrix[top][i]);
            }
            for (int i = top; i < bottom; i++) {
                list.add(matrix[i][right]);
            }
            for (int i = right; i > left; i--) {
                list.add(matrix[bottom][i]);
            }
            for (int i = bottom; i > top; i--) {
                list.add(matrix[i][left]);
            }
            right--;
            left++;
            top++;
            bottom--;

        }

        return list;
    }


    @Test
    public void test() {
        // 测试代码

        spiralOrder(new int[][]{{6,7,9}});
        spiralOrder(new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}});
    }

}
